package ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ssm.entity.Account;
import ssm.service.AccountService;

import java.util.List;

//账户controller
@Controller
public class AccountController {

    //注入账户service
    @Autowired
    private AccountService accountService;

    //查询账户列表数据
    @RequestMapping("/list.do")
    public ModelAndView list(){
        ModelAndView mav = new ModelAndView();
        /*//创建ArrayList集合对象
        ArrayList<Account> accountList = new ArrayList<>();
        //向账户中添加数据
        accountList.add(new Account(1, "小明", 1000F));
        accountList.add(new Account(2, "小华", 5000F));*/

        List<Account> accountList = accountService.findAllAccount();

        //封装响应的模型数据
        mav.addObject("accountList", accountList);

        //封装响应的视图页面
        mav.setViewName("list");
        //返回模型
        return mav;
    }

    //跳转到添加账户页面
    @RequestMapping("/add.do")
    public String add(){
        return "add";
    }

    //保存添加账户信息
    @RequestMapping("/saveAccount.do")
    public String saveAccount(Account account){
        //保存账户
        accountService.saveAccount(account);
        return "redirect:list.do";
    }
}
